<template>
  <view style="padding:200rpx">
    <view @click="doTest">测试点我</view>
    <view v-for="(item,index) in showDataList">
      <u-image :src="item" :width="100" :height="100"></u-image>
      <view>{{ item }}</view>
    </view>
  </view>
</template>
<script>

// #ifdef APP-PLUS
import {HTTP_REQUEST_URL} from "@/config/app";
// #endif
export default {
  data() {
    return {
      showDataList: [],
      action: HTTP_REQUEST_URL + '/common/upload',
    };
  },
  onLoad() {

  },
  methods: {
    // #ifdef APP-PLUS
    doTest() {
      const lemonjkFileSelect = uni.requireNativePlugin('lemonjk-FileSelect');
      //提示：
      //1.不要忘了在 "manifest.json -> APP原生插件配置" 内添加本插件，否则云打包（离线打包）不会生效
      lemonjkFileSelect.showPicker({
        //各属性配置见下方【showPicker可配置参数说明】
        pathScope: "/Download",
        mimeType: "*/*",
        utisType: "public.data",
        multi: 'yes',
        // securityPackageName: "com.android.egg",
        // securityScope: 'data'
      }, result => {
        // 返回值说明见下方【showPicker返回值说明】
        console.log('result', result)
        // （仅安卓）当错误码为1001，即未授权文件读取权限,可以提示用户未打开读取文件权限，并跳转设置页
        if (result.code == 1001) {
          uni.showModal({
            title: "需要文件访问权限",
            content: "您还未授权本应用读取文件。为保证您可以正常上传文件，请在权限设置页面打开文件访问权限（不同手机厂商表述可能略有差异）请根据自己手机品牌设置",
            confirmText: "去授权",
            cancelText: "算了",
            success(e) {
              if (e.confirm) {
                // 跳转到应用设置页
                lemonjkFileSelect.gotoSetting();
              }
            }
          })
        }
        const tempFilePaths = result.files
        for (let i = 0; i < tempFilePaths.length; i++) {
          uni.showLoading({
            title: 'loading...'
          })
          this.uploadFile(tempFilePaths[i].filePath, tempFilePaths[i].filePath)
        }
      })
    },
    // #endif
    uploadFile(file, showFile) {
      const that = this
      this.showDataList.push(showFile)
      uni.uploadFile({
        url: that.action,
        filePath: file,
        name: 'file',
        success: (uploadFileRes) => {
          console.log('uploadFileRes', uploadFileRes)
          const resData = JSON.parse(uploadFileRes.data)
          uni.showToast({
            title: $t('操作成功')
          })
          uni.hideLoading()
          if (resData.code === 1) {
            console.log('resData', resData)
          } else {
            that.$u.toast(resData.msg)
          }
        }
      });
    },
  }
}
</script>

<style lang="scss">

</style>
